# -*- coding: utf-8 -*-
"""
@File:    title_extraction.py
@Author:  jk.dong
@Desc:    None
"""
import pickle
import re
import jieba
# import jieba.posseg
from jieba import Tokenizer
from jieba.posseg import POSTokenizer


class KeyInfoFind(object):
    def brackets_reduce(self, matched):
        """
        处理括号，将不含比例的括号内容去掉
        """
        m = matched.group('value')
        if len(m) == 4:
            if re.search('\d', m):
                return '*' + m[-1]
            return '——' + m[-1]
        elif not re.search(r"[\u4e00-\u9fa5]", m):
            if re.search(r"\([036]\d{4,5}\)", m):
                return ''
            elif re.search(r"warning|spam", m, re.I):
                return ''
            elif not re.search(r"[a-zA-Z]", m) and '-' in m:
                return m.replace('-', '@&@')
        elif re.search(r"垃圾邮件", m) and len(m) < 12:
            return ''
        elif m[0] == '【' and m[-1] == '】' and '银河' in m:
            return m + '*'
        return m

    def staff_judge(self, sen, name_stop):
        man = []
        if not re.sub(r"[\u4e00-\u9fa5\u0020,、，_]", '', sen):
            sens = [s for s in re.split(r"[\u0020,、，_]", sen) if s]
            stop = [s for s in sens if s in name_stop]
            lenth = [s for s in sens if len(s) not in [2, 3]]
            if not lenth and not stop and len(sens) > 1:
                man = [','.join(sens), sen]
            elif len(sens) == 1 and '团队' in sens[0] and len(sens[0]) in [4, 5]:
                man = [','.join(sens), sen]
        return man

    def staff(self, strin, a_not, name_stop):
        man = []
        for sp in a_not:
            m = self.staff_judge(sp, name_stop)
            if m:
                man.append(m)
        if len(man) == 1:
            if man[0][1] == a_not[-1]:
                return man[0] + [1]
            return man[0] + [0]

        man = []
        sp1 = [sen for sen in re.split(r'[*]', strin) if sen and re.search(r"[\w]", sen)]
        for sp in sp1:
            m = self.staff_judge(sp, name_stop)
            if m:
                man.append(m)
        if len(man) == 1:
            return man[0] + [0]

    def title_replace(self, title):
        strin = ''
        title = re.sub(r"(?P<value>[【\[].*?[】\]])", self.brackets_reduce, title)
        # print(title)
        title = re.sub(r"^(EXTERNAL:_|XNCF_)", '', title)
        title = re.findall(r"[\w\[\uff08\u0028【].*", title)
        if not title:
            return ''
        title = title[0]
        if '中金' in title[:4]:
            title = title.replace('[CICC]', '**[CICC]**').replace('_', ' ')
            title = title.replace('|', '**') if '[CICC]' in title else title.replace('|', '——')

        if '招商港股研究' in title and not re.search(r"[*_]", title):
            ind = title.index('招商港股研究')
            front = title[:ind]
            behind = title[ind + 6:]
            if not re.search(r"\w", re.sub(r"[\uff08\u0028].*?[\uff09\u0029]", '', front)):
                fr = re.search(r"[\uff08\u0028].*?[\uff09\u0029]", front)
                front = fr.group() if fr else ''
                strin += front + '*' + '招商（香港）' + '*'
                if re.search(r"\[.*\]", behind):
                    span = re.search(r"\[.*\]", behind).span()
                    strin += behind[:span[0]] + '*' + behind[span[0]:span[1]] + '*' + behind[span[1]:]
                else:
                    strin += behind
                return strin
        elif re.search(r"【招商.*?】", title) and '*' not in title:
            title = title.replace('__', '——')
            span = re.search(r"【招商.*?】", title).span()
            s1 = title[:span[0]]
            s2 = title[span[0] + 1:span[1] - 1]
            s3 = title[span[1]:]
            strin = s1 + '*' if re.search(r"[\u4e00-\u9fa5\d]", s1) else ''
            if len(s2) > 4 and not re.search(r"[\u4e00-\u9fa5]", s2[4]):
                strin = strin + '招商证券' if any(i not in s2[:4] for i in ['香港', '基金', '期货']) else strin + s2[:4]
                strin += '*' + s3 + '*' + s2[5:].replace('-', ',')
            else:
                strin = strin + '招商证券' if any(i not in s2 for i in ['香港', '基金', '期货']) else strin + s2
                strin += '*' + s3
            return strin
        elif '中金' in title[:4] and '[CICC]' in title:
            title = re.sub(r"(?P<value>\*.{4,}?\*)", self.brackets_reduce, title)
        elif '中金' in title[:4] and '：' in title:
            index = title.index('：')
            s1 = re.split(r"\*", title[:index])
            s2 = re.split(r"\*", title[index + 1:])
            if len(s1) > 1 and not [len(s) for s in re.split(r"[\s,，、]", s1[-1]) if len(s) not in [2, 3]]:
                strin += '*'.join(s1[:-1]) + '*'
                s2.append(s1[-1])
                if len(s2) > 1 and not re.search(r"[^A\u4e00-\u9fa5]", s2[0]):
                    if s2[0] not in s2[1] and s2[0] not in ['简评', '招投标备忘录', '量化策略专题', '专题研究']:
                        s2[1] = s2[1] + '——' + s2[0]
                        del s2[0]
                strin += '*'.join(s2)
                return strin
        elif 'Guotai_Junan_(Hong_Kong)' in title:
            title = title.replace('_', ' ').replace('国泰君安(香港)', '*国泰君安(香港)')
            title = '国泰君安国际*' + title
        elif ('中泰证券' in title or '中泰国际' in title) and '*' not in title:
            title = re.sub(r"(?P<value>[(（].*?[)）])", self.brackets_reduce, title)
            title = title.replace('-', '*')
            title = title.replace('@&@', '-')
        elif '银河证券' in title and '*' in title and re.search(r"【.*?】", title):
            title = re.sub(r"(?P<value>【.*?】)", self.brackets_reduce, title)
        elif '中银国际' in title:
            title = title.replace('中银国际证券', '中银国际').replace(';', ',')
        elif '兴业证券' in title:
            title = title.replace('_兴业证券', '*兴业证券').replace('_', ' ')
        elif '南方基金' in title:
            title = title.replace('南方基金——', '南方基金*')
        if '第一上海' in title:
            index = [i for i, w in enumerate(title) if w in ['(', '（']]
            index = index if index else [len(title)]
            t1 = title[:index[0]]
            t2 = title[index[0]:]
            t1 = t1.replace('第一上海证券_', '第一上海证券*').replace('-', '*').replace('_', ' ')
            title = t1 + t2
        if '元富证券' in title:
            title = title.replace('-', '*')

        return title


class OriginTitleExtractor(KeyInfoFind):
    """
    原始标题抽取器
    """
    def __init__(self, model_path):
        self.schema = ['人名','机构','投资评级','报告大类','报告小类','标题','日期']
        self._model_dir_path = model_path
        self.model_path = self._model_dir_path / 'model_best'
        self.company_path = self._model_dir_path / 'a_company.txt'  # 公司全称、简称
        self.date_path = self._model_dir_path / 'date.txt'
        self.pname_path = self._model_dir_path / 'pname.txt'  # 分析师名称
        self.doc_big_class_path = self._model_dir_path / 'big_class.txt'  # 报告大类
        self.doc_type_path = self._model_dir_path / 'a_doc_type.txt'  # 报告小类
        self.ins_name_path = self._model_dir_path / 'a_ins_name.txt'  # 研究机构
        self.word_dict = self._model_dir_path / 'word_dict.pickle'  # 词典
        self.my_model = self._model_dir_path / 'model_best'
        self.classify = self._model_dir_path / 'classify.pickle'  # 报告大类、小类关系
        self.dict_ins_path = self._model_dir_path / 'dict_ins.pickle'
        self.dict_path = self._model_dir_path / 'dic.txt'
        self.user_dict_path = self._model_dir_path / 'user_dict.txt'
        self.name_stop_path = self._model_dir_path / 'name_stop.txt'
        self.ins_half_path = self._model_dir_path / 'ins_half.txt'
        self.ins_transfer_path = self._model_dir_path / 'ins_transfer.pickle'
        self.stkcode_path = self._model_dir_path / 'stkcode.txt'  # 股票代码
        self.stkname_path = self._model_dir_path / 'stkname.txt'  # 股票
        self.scode_name_path = self._model_dir_path / 'scode_name.pickle'
        with open(self.word_dict, 'rb') as f:
            word2idx = pickle.load(f)
        with open(self.classify, 'rb') as f:
            d_new = pickle.load(f)
        with open(self.dict_ins_path, 'rb')as f:
            dict_ins = pickle.load(f)
        with open(self.name_stop_path, 'r', encoding='utf-8')as f:
            self.name_stop = [n.strip('\n') for n in f.readlines()]
        with open(self.ins_transfer_path, 'rb')as f:
            self.ins_transfer = pickle.load(f)
        with open(self.scode_name_path, 'rb') as f:
            self.scode_name = pickle.load(f)
        self.sname_code = {}
        for k, v in self.scode_name.items():
            self.sname_code.setdefault(v, [])
            self.sname_code[v].append(k)

        self.d_new = d_new
        self.word2idx = word2idx
        self.dict_ins = dict_ins
        self.jieba_tokenizer = Tokenizer()
        self.pos_cut = POSTokenizer(self.jieba_tokenizer)
        self.init_app()
        self.model = None

    def init_app(self):
        jieba.re_userdict = re.compile('^(.+?)( [0-9]+)?( [a-z_]+)?$', re.U)
        jieba.re_han_default = re.compile("(.+)", re.U)
        jieba.re_han_cut_all = re.compile("(.+)", re.U)
        jieba.posseg.re_han_internal = re.compile("(.+)")
        self.jieba_tokenizer.load_userdict(self.company_path)
        self.jieba_tokenizer.load_userdict(self.date_path)
        self.jieba_tokenizer.load_userdict(self.pname_path)
        self.jieba_tokenizer.load_userdict(self.doc_type_path)
        self.jieba_tokenizer.load_userdict(self.ins_name_path)
        self.jieba_tokenizer.load_userdict(self.doc_big_class_path)
        self.jieba_tokenizer.load_userdict(self.dict_path)
        self.jieba_tokenizer.load_userdict(self.user_dict_path)
        self.jieba_tokenizer.load_userdict(self.ins_half_path)
        self.jieba_tokenizer.load_userdict(self.stkcode_path)
        self.jieba_tokenizer.load_userdict(self.stkname_path)

    def date_find(self, sen):
        err1 = r"2\d{3}\.\d{1,2}\.\d{1,2}[~-](?:2\d{3}\.\d{1,2}\.\d{1,2}|\d{1,2}\.\d{1,2})"
        err2 = r"2\d{7}[_~-]2\d{7}|\d{6}[_~-]\d{6}"
        cor1 = r"2\d{3}-\d{2}-\d{2}[\u0020_]\d{2}:\d{2}:\d{2}(?:\.\d|)"
        cor2 = r"2\d{3}-\d{1,2}-(?:0[1-9]|[12]\d|30|31|[1-9])"
        cor3 = r"2\d{3}\.\d{1,2}\.(?:0[1-9]|[12]\d|30|31|[1-9])"
        cor4 = r"2\d{3}年\d{1,2}月(?:0[1-9]|[12]\d|30|31|[1-9])日?"
        cor5 = r"\d{2}-\d{2}-2\d{3}"
        cor6 = r"\d{2}年\d{1,2}月(?:0[1-9]|[12]\d|30|31|[1-9])日?"

        temp = []
        times = re.findall(r"\d[-:.~_年月日\d\u0020]+", sen)
        if times:
            for d in times:
                if d[0] in ['0', '3', '6'] and len(d.strip('.')) in [5, 6]:
                    pass
                elif d.count('年') > 1 or d.count('月') > 1 or d.count('日') > 1 or len(d) < 4:
                    pass
                else:
                    temp.append(d)
        if not temp:
            return ''
        date1 = re.findall(cor1, sen)
        if date1 and len(set([d[:10] for d in date1])) == 1:
            for d in date1:
                self.strin = self.strin.replace(d, '')
            return date1[0]
        date0 = re.findall(err1, sen) or re.findall(err2, sen)
        if date0:
            return ''
        date2 = re.findall(cor2, sen) + re.findall(cor3, sen) + re.findall(cor4, sen) + re.findall(cor5, sen)
        date3, date4 = [], []
        for d in re.findall(r"\d+", sen):
            if len(d) == 8 and re.search(r"20\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[12]\d|30|31)", d):
                date3.append(d)
            elif len(d) == 6 and re.search(r"[12]\d{1}(?:0[1-9]|10|11|12)(?:0[1-9]|[12]\d|30|31)", d):
                date4.append(d)
        date5 = re.findall(cor6, sen)
        date = date2 or date3 or date4 or date5
        if len(set(date)) == 1:
            return date[0]
        elif date:
            new_date = []
            for d in date:
                d = re.sub(r"[-.年月日]", '', d)
                new_date.append(d)
            if len(new_date) == 1:
                return date[0]
            else:
                return ''
        return ''

    def time_format(self, str_time):
        # 日期格式规范 2017-08-14  待优化
        if ':' in str_time:
            str_time = re.search(r"2\d{3}-\d{2}-\d{2}", str_time)
        str_time = str_time.replace('.', '-').replace('/', '-').replace('年', '-').replace('月', '-'). \
            replace('日', '').replace(' ', '-')
        list_time = re.split('-', str_time)
        year, moth, day, date = '', '', '', ''
        if len(list_time) == 3:
            if len(list_time[0]) == 4:
                year = list_time[0]
                day = list_time[2]
            elif len(list_time[2]) == 4:
                year = list_time[2]
                day = list_time[0]
            elif list_time[0] in ['17', '18', '19', '20', '21', '22', '23']:
                year = '20' + list_time[0]
                day = list_time[2]
            elif list_time[2] in ['17', '18', '19', '20', '21', '22', '23']:
                year = '20' + list_time[2]
                day = list_time[0]
            moth = list_time[1]
            if len(moth) < 2:
                moth = '0' + moth
            if len(day) < 2:
                day = '0' + day

        else:
            if len(str_time) in [8, 9] and str_time[:4] in ['2017', '2018', '2019', '2020', '2021', '2022', '2023']:
                year = str_time[:4]
                moth = str_time[4:6]
                day = str_time[-2:]
            elif len(str_time) == 6:
                if str_time[:2] in ['17', '18', '19', '20', '21', '22', '23']:
                    year = '20' + str_time[:2]
                    day = str_time[-2:]
                elif str_time[-2:] in ['17', '18', '19', '20', '21', '22', '23']:
                    year = '20' + str_time[-2:]
                    day = str_time[:2]
                moth = str_time[2:4]

        date = year + '-' + moth + '-' + day
        if len(date) != 10:
            date = ''
        return date

    def ins_find(self, a_ins_wt, a_ins_wt2):
        research_ini = ''
        if 'u_ins_name' in a_ins_wt2:
            research_ini = [s[0] for s in a_ins_wt if s[1] == 'u_ins_name'][0]
        elif 'u_ins' in a_ins_wt2:
            ins = [s[0] for s in a_ins_wt if s[1] == 'u_ins'][0]
            for k, v in self.dict_ins.items():
                if ins in v:
                    research_ini = k
        return research_ini

    def name_find(self, a_ins_wt, a_ins_wt2):
        name = []
        if set(['nr', 'p_name', 'nrfg']) & set(a_ins_wt2):
            name.extend([s[0] for s in a_ins_wt if s[1] in ['nr', 'p_name', 'nrfg'] and len(s[0]) > 1])
        return name

    def ini_find(self, name, a_ins_wt, a_ins_wt2):
        research_ini = ''
        if name in a_ins_wt2:
            research_ini = [s[0] for s in a_ins_wt if s[1] == name][0]
        return research_ini

    def del_emp(self, lis):
        lis = [t for t in lis if t]
        lis = sorted(lis, key=lambda x: len(x))
        if lis:
            return lis[0]
        else:
            return ''

    def ini_half_inpact_find(self, strin):
        actual_ini = []
        cuts = [(w, f) for w, f in self.pos_cut.cut(strin) if f in ['ins', 'u_ins_name']]
        if cuts:
            w, f = cuts[0]
            actual_ini.append(w)
            if f == 'u_ins_name':
                return w, actual_ini
            else:
                for k, v in self.ins_transfer.items():
                    if w in v:
                        return k, actual_ini

    def stkcode_find(self, strin):
        scode = []
        actual_scode, actual_sname = [], []
        for t in re.findall(r"\d+", strin):
            if len(t) in [5, 6] and t[0] in ['8', '1', '6', '2', '9', '4', '0', '3']:
                if self.scode_name.get(t):
                    scode.append(t)
                    actual_scode.append(t)
                    actual_sname.append(self.scode_name.get(t))
        if not scode:
            for t in re.findall(r"\d+[.\s]HK", strin):
                t1 = re.search(r"\d+", t).group()
                t1 = '0' + t1
                if len(t1) == 5 and self.scode_name.get(t1):
                    scode.append(t1)
                    actual_scode.append(t)
                    actual_sname.append(self.scode_name.get(t1))

        if len(set(scode)) == 1:
            scode = scode[0]
            sname = self.scode_name[scode]
            return scode, sname, actual_scode, actual_sname
        return '', '', actual_scode, actual_sname

    def extract_grade(self, title, scode, sname, r_date):
        grade = ''
        exp_grade_w = "((?:强[烈力]|审慎)?(?:买入|增持|推荐|中性|观望|减持|卖出|强于大市)(?:看多)?)"
        exp_grade_tail = "(?:评级|[-:：_*)）|，—；;]|$)"
        exp_grade1 = r"(?:维持|首次覆盖(?:给予)?|重申|[上下]调(?:评级)?至)" + exp_grade_w + exp_grade_tail
        exp_grade2 = r"[-*(（_）):：,，—；;]" + exp_grade_w + exp_grade_tail

        for ele in scode + sname:
            title = title.replace(ele, '')
        title = title.replace(r_date, '')
        p_line = re.sub(r"[\s“”]", '', title)
        res_exp1 = list(re.finditer(exp_grade1, p_line))
        res_exp2 = list(re.finditer(exp_grade2, p_line))
        if len(res_exp1 + res_exp2) == 1:
            for r in res_exp1 + res_exp2:
                grade = r.group(1)
                print('grade', grade)
        return grade

    def extraction(self, strin):  # 根据词性抽取
        # 备注：ns虽然是表示地方，但是，有标注错误的时候，比如 广证恒生 苏培海 u_ins_name，ns
        name_flags = ['p_name', 'nr', 'nrfg', 'x', 'ns']
        name_list, title, research_ini, doc_type, doc_big_type, r_date, ini_intact = '', '', '', '', '', '', ''
        # 匹配股票代码、股票名称
        scode, sname, actual_scode, actual_sname = self.stkcode_find(strin)
        self.strin = self.title_replace(strin)
        # 匹配证券机构 tuple: ('申万宏源证券', ['申万宏源证券'])
        ini = self.ini_half_inpact_find(self.strin)
        if ini:
            ini_intact, actual_ini = ini
        else:
            ini_intact, actual_ini = '', []
        # 查找日期
        r_date = self.date_find(self.strin)
        grade = self.extract_grade(self.strin, actual_scode, actual_sname, r_date)
        # 将原始标题根据[*_]这两个字符切分成几段，按照长短排序
        # ['申万宏源证券', '申银万国', '行业点评', '林起贤,袁伟嘉', ' 监管环境边际改善拉动影视反弹，游戏迎政策利好-互联网传媒周报，19/12/9-19/12/13']
        a_not = [sen for sen in re.split(r'[*_]', self.strin) if sen and re.search(r"[\w]", sen)]
        if len(a_not) > 1:
            delte = re.search(r"[\uff08\u0028].*", a_not[-1])
            if delte and not re.search(r"[a-zA-Z\u4e00-\u9fa5]", delte.group()):
                a_not[-1] = re.search(r"(.*?)[\uff08\u0028]", a_not[-1]).group(1)
        man_pos = self.staff(self.strin, a_not, self.name_stop)
        if man_pos:
            name_list = man_pos[0]
            if man_pos[2] == 1:
                a_not = a_not[:-1]
            else:
                a_not = [a for a in a_not if a != man_pos[1]]

        a_not = sorted([a for a in a_not if a], key=lambda x: len(re.sub(r"[^a-zA-Z\u4e00-\u9fa5]", '', x)),
                       reverse=True)
        s_test = list(self.jieba_tokenizer.cut(self.strin))
        # pad_sequences 将序列处理成同等长度,默认从前面截取，填充0  默认长度是27；word2idx.get(w, 0),将jieba分词转成id
        # x_test_sent = pad_sequences(sequences=[[self.word2idx.get(w, 0) for w in s_test]],
        #                             padding="post", value=0, maxlen=27)
        # enumerate用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标
        # 过滤a_not找出长度大于4，并且可以再分词【即排除证券机构 报告类型等】的短语,最多取3个;isdigit() 方法检测字符串是否只由数字组成
        a_sen_like = [sen for sen in a_not if len(sen) > 4 and len(list(self.jieba_tokenizer.cut(sen))) > 1 and
                      not sen.isdigit()][:3]
        # 将a_sen_like 中的短句切词
        a_sen_like_wt = [[(word, flag) for word, flag in self.pos_cut.cut(line)] for line in a_sen_like]

        # a_not=a_sen_like+a_others
        a_others = [ele for ele in a_not if ele not in a_sen_like]
        # print(a_others)
        # ini:证券机构 name:研究人员 doc_type_list:报告小类
        ini, name, doc_type_list, doc_big, ini_half = [], [], [], [], []
        # 第一种情况 原始标题没有分隔符
        if len(a_not) == 1 and s_test[0] == '【' and s_test[-1] != '】' and '】' in s_test:
            a_ins = re.findall('^【.*?】', a_not[0])[0]
            string1 = re.findall('（.*）', a_not[0])
            other = a_not[0][len(a_ins):]
            string1.append(a_ins)
            string2 = [a_ins, other]
            # ini, name, date, doc_type_list, doc_big = [], [], [], [], []
            for tmp in string1:
                str_wt = [(word, flag) for word, flag in self.pos_cut.cut(tmp)]
                _, str_wt2 = zip(*str_wt)
                ini.append(self.ins_find(str_wt, str_wt2))
                doc_type_list.append(self.ini_find('u_doc_type', str_wt, str_wt2))
                doc_big.append(self.ini_find('u_big_class', str_wt, str_wt2))
            for tmp in string2:
                str_wt = [(word, flag) for word, flag in self.pos_cut.cut(tmp)]
                _, str_wt2 = zip(*str_wt)
                name.extend(self.name_find(str_wt, str_wt2))
            research_ini = self.del_emp(ini)
            doc_type = self.del_emp(doc_type_list)
            doc_big_type = self.del_emp(doc_big)
            title = other.replace('()', '').replace('（）', '')  # .replace(r_date, '')
        else:
            # 第二种情况，有分隔符，从a_sen_like_wt中找name,ini,title, 从other找doc_type,doc_big_type,name,ini
            if a_sen_like_wt:
                for ele in a_sen_like_wt:
                    # ele = [i for i in ele if i[0] != r_date]
                    ele = ele[:-1] if ele[-1][0] == r_date else ele
                    if not ele:
                        continue
                    list_1, list_2 = zip(*(ele))
                    # 匹配研究员姓名
                    if set(list_2) <= set(name_flags):
                        name = [i[0] for i in ele if i[1] != 'x']
                    elif ('nr' in list_2 or 'p_name' in list_2):
                        nr_index = [i for i, e in enumerate(list_2) if e == 'nr' or e == 'p_name']
                        name = [e for i, e in enumerate(list_1) if i in nr_index]
                        other = [e for i, e in enumerate(list_1)]
                        if len(other) > 3 and len(''.join(other)) > len(title):
                            title = ''.join(other)
                    elif len(set(list_2)) > 2 and len(list_1) > 3 and len(''.join(list_1)) > len(title):
                        title = ''.join(list_1)
                    if 'u_ins_name' in list_2 and research_ini == '':
                        research_ini = [s[0] for s in ele if s[1] == 'u_ins_name'][0]

                if title == '':
                    title = a_sen_like[0]
                title = title.replace('()', '').replace('（）', '')  # .replace(r_date, '')

            if a_others:
                name_f = []
                for ele in a_others:
                    str_wt = [(word, flag) for word, flag in self.pos_cut.cut(ele)]
                    _, str_wt2 = zip(*str_wt)
                    ini.append(self.ini_find('u_ins_name', str_wt, str_wt2))
                    doc_type_list.append(self.ini_find('u_doc_type', str_wt, str_wt2))
                    doc_big.append(self.ini_find('u_big_class', str_wt, str_wt2))
                    name_f.extend(self.name_find(str_wt, str_wt2))
                if name_f:
                    name = name_f
                if self.del_emp(ini) != '':
                    research_ini = self.del_emp(ini)
                doc_type = self.del_emp(doc_type_list)
                doc_big_type = self.del_emp(doc_big)

        title = title.strip()
        if r_date:
            rd = "[（\(]?" + re.sub(r"[^0-9]", '', r_date[:10]) + "[）\)]?"
            title = re.sub(rd + "$", '', title)

        # 如果只找到了报告小类 从词典里对应取出报告大类
        if not doc_big_type and doc_type:
            for k, v in self.d_new.items():
                if doc_type in v:
                    doc_big_type = k


        if '申银万国' in research_ini:
            research_ini = '申万宏源研究'
        research_ini = research_ini.replace('申万宏源证券', '申万宏源研究')
        if not research_ini and ini_half:
            research_ini = ini_half[0]
        # , actual_ini, actual_scode, actual_sname, actual_date
        my_ie = Taskflow("information_extraction", schema=self.schema, task_path=self.model_path)
        uie_result = my_ie(strin)
        # ['人名','机构','投资评级','报告大类','报告小类','标题','日期']
        if not name_list and uie_result[0].get('人名'):
            name_list = uie_result[0].get('人名')
        return {'机构': research_ini, '研究员': name_list,
                '报告小类': doc_type, '报告大类': doc_big_type,
                '报告日期': r_date, '标题': title,
                '股票代码': scode, '股票名称': sname,
                '投资评级': grade}



